#include "Arduino.h"

#include "stdlib.h"

#define LED1 2 //定义led1脚位常量
#define LED2 3 //定义led2脚位常量
#define LED3 4 //定义led3脚位常量
#define LED4 5 //定义led4脚位常量
#define LED5 6 //定义led5脚位常量
#define LED6 7 //定义led6脚位常量
#define LED7 8 //定义led7脚位常量




#define INPUT_BUTTON A0 //定义输入脚位

int no_times = 0; //初始化按钮次数

bool check_button = false; //检测是否按下按钮
// 初始化
void setup() {
  // 串口输出 设置波特率
  Serial.begin(115200);

  // 按钮初始化
  pinMode(INPUT_BUTTON, INPUT_PULLUP);

  //初始化led1-led3灯为输出模式以及低电平
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);


  //串口打印输出信息
  Serial.println("初始化完成.");
}

void led_delay (int pin_no , int delaytime)
{
  digitalWrite(pin_no, HIGH);
  delay(delaytime);
  digitalWrite(pin_no, LOW);
  Serial.print("引脚-"); Serial.print(pin_no); Serial.print("开了"); Serial.print(delaytime); Serial.println("毫秒");

}

void run_led(int times)
{
  for (int x = 0; x <= times; x++) {
    for (int j = 1; j <= 10; j++) {
      led_delay (LED1, 20 * j);
      led_delay (LED2,  20 * j);
      if (j % 3 == 1) {
        led_delay (LED3,  20 * j);
      }
      led_delay (LED4,  20 * j);
      led_delay (LED5,  20 * j);
      if (j % 3 == 2) {
        led_delay (LED6,  20 * j);
      }
      led_delay (LED7,  20 * j);
      Serial.print("j："); Serial.println(j);
    }
    Serial.print("次数："); Serial.println(times);
  }
}

//循环程序开始
void loop() {


  //按钮判断是否按下去
  if (digitalRead(INPUT_BUTTON) == LOW) {
    check_button = true;
  }

  int rx = random(10) ;
  run_led(rx);
  /**
    if ( check_button == true) {
      no_times += 1;
      Serial.print("按钮按下第("); Serial.print(no_times); Serial.println(")次");
      if ( no_times == 0 ) {
        ;
      }
      else if ( no_times == 1 ) {
        run_led(1);
      }
      else if ( no_times == 2 ) {
        run_led(2);
      }
      else if ( no_times == 3 ) {
        run_led(3);
        no_times = 0;
      }

      check_button = false;
    }
  **/

  delay(10);
}
